//index.js
//引入高德地图sdk
var amapFile = require('../../libs/amap-wx.js'); //加载高德地图sdk
var markersData = []; //创建mark点，我们要创建多个点，所以这里是一个数组

//配置地图key 设置成全局配置 方便下面调用
var myAmapFun = new amapFile.AMapWX({
	key: 'a038f50d26bc347a0fa1b50834ad27dc'
});

Page({
	data: {
		markers: [],
		latitude: '',
		longitude: '',
		textData: {},
		scale: 16, //缩放等级 默认是16 但是坐地铁的时候 需要加大视野
		currentMenu: 1, //menu
		polyline: [] ,//路径点
		distance:0 //距离目标地点有多远
	},
	getmap: function(e) {
		//选中菜单点击项
		this.setData({
			currentMenu: e.target.dataset.menu
		})
		//切换时重设中心点
		this.setFocus();
		//如果是坐地铁，修改缩放等级
		if (e.target.dataset.menu == 4) {
			this.setData({
				scale: 14
			})
		} else {
			this.setData({
				scale: 16
			})
		}
		//重置地图
		this.queryMap(e.target.dataset.place);
		//清空路径
		this.clearRoute();
	},
	//点击mark触发的事件
	makertap: function(e) {
		//e.markerId 当前点击的是哪个marker
		var id = e.markerId;
		var that = this;
		that.showMarkerInfo(markersData, id);
		that.changeMarkerColor(markersData, id);

		//传入markerId 用来获取终点的位置
		this.getRoute(e.markerId);
	},
	onLoad: function() {
		//设置地图中心点
		this.setFocus();
		//查询附近的点 默认查询图文印刷
		this.queryMap("图文印刷");
		
		//获得帮助提示框组件
		let compData=this.selectComponent("#help");
		compData.powerDrawer();
	},
	//设置地图中心点
	setFocus() {
		let that = this;
		wx.getLocation({
			type: 'gcj02',
			success: function(res) {
				that.setData({
					latitude: res.latitude
				});
				that.setData({
					longitude: res.longitude
				});
				that.setData({
					city: '北京市'
				});
			},
			fail: function() {
				that.setData({
					latitude: 39.909729
				});
				that.setData({
					longitude: 116.398419
				});
				that.setData({
					city: '北京市'
				});
			}
		})
	},
	//查询附近的点
	queryMap(place) {
		let that = this;
		myAmapFun.getPoiAround({
			querykeywords: place,
			iconPathSelected: '../../img/marker_checked.png', //选中时的图标
			iconPath: '../../img/marker.png', //未选中时的图标
			iconWidth: 22,
			iconHeight: 32,
			success: function(data) {
				//返回附近地点的坐标
				markersData = data.markers;
				//拿到坐标以后发给page
				that.setData({
					markers: markersData
				});
				//console.log(markersData);
				//显示第一个数据的详情
				that.showMarkerInfo(markersData, 0);
			},
			fail: function(info) {
				wx.showModal({
					title: info.errMsg
				})
			}
		})
	},
	//把点对应的详情显示到下面
	showMarkerInfo: function(data, i) {
		var that = this;
		that.setData({
			textData: {
				name: data[i].name,
				desc: data[i].address
			}
		});
	},
	//当你选中一个点以后，把当前的点改成红色的，其他的点改成蓝色的
	changeMarkerColor: function(data, i) {
		var that = this;
		var markers = [];
		for (var j = 0; j < data.length; j++) {
			if (j == i) {
				data[j].iconPath = "../../img/marker_checked.png"; //选中
			} else {
				data[j].iconPath = "../../img/marker.png"; //未选中
			}
			markers.push(data[j]);
		}
		that.setData({
			markers: markers
		});
	},
	//绘制路径
	getRoute(markerId) {
		let that = this;
		let startLongitude = that.data.longitude; //获取当前位置的经度
		let startLatitude = that.data.latitude; //获取当前位置的纬度
		let origin = startLongitude + "," + startLatitude; //拼接成最终的起点数据

		let endLongitude = markersData[markerId].longitude; //获取当前位置的经度
		let endLatitude = markersData[markerId].latitude; //获取当前位置的纬度
		let destination = endLongitude + "," + endLatitude; //拼接成最终的终点数据

		myAmapFun.getDrivingRoute({
			origin: origin,
			destination: destination,
			//origin: '39.9219,116.44355', //开始位置
			//destination: '116.434446,39.90816', //结束位置
			success: function(data) {
				//console.log(data);
				var points = [];
				if (data.paths && data.paths[0] && data.paths[0].steps) {
					var steps = data.paths[0].steps;
					for (var i = 0; i < steps.length; i++) {
						var poLen = steps[i].polyline.split(';');
						for (var j = 0; j < poLen.length; j++) {
							points.push({
								longitude: parseFloat(poLen[j].split(',')[0]),
								latitude: parseFloat(poLen[j].split(',')[1])
							})
						}
					}
				}
				that.setData({
					polyline: [{
						points: points,
						color: "#0091ff",
						arrowLine:true, //显示箭头线，注意这个在pc模拟器中显示不出来，手机中可以
						width: 6
					}]
				});
				if (data.paths[0] && data.paths[0].distance) {
					that.setData({
						distance: data.paths[0].distance + '米'
					});
				}
				// if(data.taxi_cost){
				//   that.setData({
				//     cost: '打车约' + parseInt(data.taxi_cost) + '元'
				//   });
				// }
			}
		})
	},
	//清除画出来的路径
	clearRoute() {
		this.setData({
			polyline: [{
				points: [],
				color: "#0091ff",
				width: 6
			}]
		})
	},
	onShareAppMessage: function () {
	    return {
	        title: '欢迎使用面试精灵！',
	        desc: '导航和日程记录，让面试简单化！',
	        path: '/pages/index/index'
	    }
	}
})
